﻿using System.Collections.Generic;
using System.Xml.Linq;

namespace Grinder.Fb2
{
    public sealed class TitleInfo
    {
        #region Private fields
        private readonly List<Author> _authors = new List<Author>();
        private readonly List<string> _genres = new List<string>();
        private readonly List<Author> _translators = new List<Author>();
        private readonly List<Sequence> _sequences = new List<Sequence>();
        #endregion

        #region Properties
        public IList<Author> Authors { get { return _authors; } }
        public IList<string> Genres { get { return _genres; } }
        public IList<Author> Translators { get { return _translators; } }
        public IList<Sequence> Sequences { get { return _sequences; } }
        public string BookTitle { get; set; }
        public string Date { get; set; }
        public string Language { get; set; }
        public string SrcLanguage { get; set; }
        #endregion

        public TitleInfo() { }

        public TitleInfo(XContainer element)
        {
            // Parsing author information
            var authorEls = element.Elements(Constants.Xmlns + "author");
            if (authorEls != null)
                foreach (var authorEl in authorEls)
                    _authors.Add(new Author(authorEl));

            // Parsing genre information
            var genreEls = element.Elements(Constants.Xmlns + "genre");
            if (genreEls != null)
                foreach (var genreEl in genreEls)
                    _genres.Add(genreEl.Value);

            // Parse translator information (zero or more)
            var transEls = element.Elements(Constants.Xmlns + "translator");
            if (transEls != null)
                foreach (var transEl in transEls)
                    _translators.Add(new Author(transEl));

            // Parsing sequence information
            var seqEls = element.Elements(Constants.Xmlns + "sequence");
            if (seqEls != null)
                foreach (var seqEl in seqEls)
                    _sequences.Add(new Sequence(seqEl));

            // Book title
            var el = element.Element(Constants.Xmlns + "book-title");
            BookTitle = (el == null) ? "" : el.Value;

            // Date
            el = element.Element(Constants.Xmlns + "date");
            Date = (el == null) ? "" : el.Value;

            // Book language
            el = element.Element(Constants.Xmlns + "lang");
            Language = (el == null) ? "" : el.Value;

            // Book source title
            el = element.Element(Constants.Xmlns + "src-lang");
            SrcLanguage = (el == null) ? "" : el.Value;

        }
    }
}
